<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
          <el-form-item label="提示码" prop="tipCode"
                :rules="[
                  {required: true, message:'提示码不能为空', trigger:'blur'},
                  {validator:checkRepeatCode, trigger:'blur'}
                 ]">
             <el-input v-model="inputForm.tipCode" placeholder="请填写提示码"     ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row  :gutter="15">
        <el-col :span="50">
          <el-form-item label="提示描述" prop="tipDescribe">
              <el-input v-model="inputForm.tipDescribe" placeholder="请填写提示描述（满足何种条件触发的提示）"     ></el-input>
           </el-form-item>
        </el-col>
      </el-row>
      <el-row  :gutter="15">
        <el-col :span="12">
          <el-form-item label="给用户的提示" prop="tipToUser"
                :rules="[
                  {required: true, message:'提示语不能为空', trigger:'blur'}
                 ]">
             <el-input v-model="inputForm.tipToUser" placeholder="请填写提示语"     ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          tipCode: '',
          tipToUser: '',
          tipDescribe: ''
        }
      }
    },
    components: {
    },
    methods: {
    	handleClose(done) {
			this.$Msg.confirm({
				title:'您确定要关闭吗？',
				content:this.GLOBAL.FORM_CLOSE_TIPS,
				confirmCall:()=>{
					done();
				}
			})
		},
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建提示语`
        } else if (method === 'edit') {
          this.title = '修改提示语'
        } else if (method === 'view') {
          this.title = '查看提示语'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/sys/tipCode/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data.success) {
                this.inputForm = this.recover(this.inputForm, data.result)
                this.loading = false
              } else {
                this.loading = false
                this.visible = false
              }
            })
          }
        })
      },
      // 提示码重复
      checkRepeatCode (rule, value, callback) {
        if (value) {
          this.$http({
            url: `/sys/tipCode/validate`,
            method: 'get',
            params: {'id': this.inputForm.id, 'tipCode': value}
          }).then(({data}) => {
            if (data.code === 'A100008') {
              callback(new Error('错误码有重复'))
            } else {
              callback()
            }
          })
        } else {
          callback()
        }
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/sys/tipCode/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.visible = false
                this.$message.success(data.message)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
